package org.fastsyncer.manager.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.fastsyncer.common.entity.MappingTask;

public class SerializableUtil {

	// 对象序列化过程
	public static void serializable(File file, Object obj) {
		try {
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 对象反序列化过程
	public static Object deSerializable(File file) {
		try {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			Object readObject = ois.readObject();
			ois.close();
			fis.close();
			return readObject;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws Exception {

		MappingTask task = new MappingTask();
		task.setName("yangpei666");
		task.setMaxTask(199);

		File file = new File("D:/fastsyncer/sourceAdmin_targetXddb/sourceAdmin_targetXddb.config");
//		SerializableUtil.serializable(file, task);
		System.out.println("ok...");
		task = (MappingTask) SerializableUtil.deSerializable(file);
		System.out.println(task);
		System.out.println(task.getProcessCompleted());
	}

}
